<?php echo $header;echo $sidebar ?>		
	
	<link rel="stylesheet" href="<?php echo base_url(); ?>assets/admin/css/new_font-awesome.css"/>
	<link href="<?php echo $this->config->base_url(); ?>assets/admin/css/jquery-ui.css" rel="stylesheet">
	<link href="<?php echo $this->config->base_url(); ?>assets/admin/css/chosen.css" rel="stylesheet">
	<link href="<?php echo $this->config->base_url(); ?>assets/admin/css/chosen-bootstrap.css" rel="stylesheet">
	<script type="text/javascript" src="<?php echo $this->config->base_url(); ?>assets/admin/js/chosen.jquery.js"></script>
	<script type="text/javascript" src="<?php echo $this->config->base_url(); ?>assets/admin/js/jquery-ui.js"></script>
	<script src="<?php echo $this->config->base_url(); ?>assets/admin/media/js/jquery.dataTables.columnFilter.js" type="text/javascript"></script>
	<style type="text/css">
	
	
	.select_filter {width: 100% !important;padding:0 !important;height: auto !important;margin:0 !important;}
	.btn-group, .btn-group-vertical {display: inline-block;position: relative;vertical-align: middle;}
	* {border-radius: 0 0 0 0 !important;box-shadow: none !important;font-family: "Open Sans",Arial,Helvetica,sans-serif;font-weight: 400;text-shadow: none !important;}
	.btn-group > .btn:first-child {margin-left: 0;}
	.btn-group a.btn {margin: 0 !important;}
	.btn.btn-primary {background: none repeat scroll 0 0 #1171A3 !important;}
	.btn-xs {padding: 1px 5px;}
	.btn-sm, .btn-xs {border-radius: 3px 3px 3px 3px;font-size: 12px;line-height: 1.5;padding: 2px 5px;}
	.tooltip, .fa {font-size: 15px;}
	.fa {display: inline-block;font-family: FontAwesome;font-style: normal;font-weight: normal;line-height: 1;}
	.btn-group, .btn-group-vertical {display: inline-block;position: relative;vertical-align: middle;}
	
	.modal-header .close {margin-top: -2px;}
	.modal-content {box-shadow: 0 5px 15px rgba(0, 0, 0, 0.5);}
	.modal-content {
		background-clip: padding-box;
	    background-color: #FFFFFF;
	    border: 1px solid rgba(0, 0, 0, 0.2);
	    border-radius: 6px 6px 6px 6px;
	    box-shadow: 0 3px 9px rgba(0, 0, 0, 0.5);
	    outline: 0 none;
	    position: relative;
	}	
	button.close {background: none repeat scroll 0 0 transparent;border: 0 none;cursor: pointer;padding: 0;}
	
	.table tfoot th:nth-child(5), .table td:nth-child(5), .table tfoot th:nth-child(6), .table td:nth-child(6), .table tfoot th:nth-child(7), .table td:nth-child(7) {text-align: right;}
	.table td:nth-child(6) {text-transform: capitalize;}
	.table td {width:10%;}
	.table td:first-child, .table td:nth-child(8) {width: 6%;}
	.table td:nth-child(8) {text-align:center;}
	.table td:nth-child(8) span { display:block; }
	.table td:nth-child(3), .table td:nth-child(5), .table td:nth-child(6), .table td:nth-child(7) {width:5%;}
	.text_filter {
		width: 100% !important;
		border: 0 !important;
		box-shadow: none !important;
		border-radius: 0 !important;
		padding:0 !important;
		margin:0 !important;
		font-size: 1em !important;
	}
	
	/* Label colors */
	.label.label-success, .badge.badge-success{background: #43c83c !important;}
	.label.label-warning, .badge.badge-warning{background: #f88529 !important;}
	.label.label-important, .badge.badge-important{background: #fa3031 !important;}
	.label.label-info, .badge.badge-info{background: #52b9e9 !important;}
	
	.col-md-6{float:left;width:47%;}
	#dataTables_filter{float:right;}
	
	.col-xs-1, .col-xs-2, .col-xs-3, .col-xs-4, .col-xs-5, .col-xs-6, .col-xs-7, .col-xs-8, .col-xs-9, .col-xs-10, .col-xs-11, .col-xs-12, .col-sm-1, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6, .col-sm-7, .col-sm-8, .col-sm-9, .col-sm-10, .col-sm-11, .col-sm-12, .col-md-1, .col-md-2, .col-md-3, .col-md-4, .col-md-5, .col-md-6, .col-md-7, .col-md-8, .col-md-9, .col-md-10, .col-md-11, .col-md-12, .col-lg-1, .col-lg-2, .col-lg-3, .col-lg-4, .col-lg-5, .col-lg-6, .col-lg-7, .col-lg-8, .col-lg-9, .col-lg-10, .col-lg-11, .col-lg-12 {
    	min-height: 1px;
    	padding-left: 15px;
    	padding-right: 15px;
    	position: relative;
	}

	</style>
	
	<script type="text/javascript" charset="utf-8">
			$(document).ready( function () {

				function status(x) {
					var st = x.split('-');
					switch (st[0]) {
						case '<?php echo $this->lang->line('sale_paid'); ?>':
							return '<a id="'+st[1]+'" href="#myModal" role="button" data-toggle="modal" class="st"><span class="label'+st[1]+' label label-success">'+st[0]+'</span></a>';
				  			break;
							
						case '<?php echo $this->lang->line('sale_partially_paid'); ?>':
							return '<a id="'+st[1]+'" href="#myModal" role="button" data-toggle="modal" class="st"><span class="label'+st[1]+' label label-info">'+st[0]+'</span></a>';
				  			break;
							
						case '<?php echo $this->lang->line('sale_pending'); ?>':
							return '<a id="'+st[1]+'" href="#myModal" role="button" data-toggle="modal" class="st"><span class="label'+st[1]+' label label-warning">'+st[0]+'</span></a>';
				  			break;
							
						case '<?php echo $this->lang->line('sale_overdue'); ?>':
							return '<a id="'+st[1]+'" href="#myModal" role="button" data-toggle="modal" class="st"><span class="label'+st[1]+' label label-danger">'+st[0]+'</span></a>';
				  			break;	
							
						case '<?php echo $this->lang->line('sale_cancelled'); ?>':
							return '<a id="'+st[1]+'" href="#myModal" role="button" data-toggle="modal" class="st"><span class="label'+st[1]+' label label-danger">'+st[0]+'</span></a>';
				  			break;		
				  
				  
						default:
				  			return '<a id="'+st[1]+'" href="#myModal" role="button" data-toggle="modal" class="st"><span class="label'+st[1]+' label label-default">'+st[0]+'</span></a>';
				  
					}
				}
				
				function format_date(oObj) {
					var aDate = oObj.split('-');
					<?php if(JS_DATE == 'dd-mm-yy') { ?>
					return aDate[2] + "-" + aDate[1] + "-" + aDate[0];
					<?php } elseif(JS_DATE == 'dd/mm/yy') { ?>
					return aDate[2] + "/" + aDate[1] + "/" + aDate[0];
					<?php } elseif(JS_DATE == 'dd.mm.yy') { ?>
					return aDate[2] + "." + aDate[1] + "." + aDate[0];
					<?php } elseif(JS_DATE == 'mm/dd/yy') { ?>
					return aDate[1] + "/" + aDate[2] + "/" + aDate[0];
					<?php } elseif(JS_DATE == 'mm-dd-yy') { ?>
					return aDate[1] + "-" + aDate[2] + "-" + aDate[0];
					<?php } elseif(JS_DATE == 'mm.dd.yy') { ?>
					return aDate[1] + "." + aDate[2] + "." + aDate[0];
					<?php } else { ?>
					return sValue;
					<?php } ?>
				}
				
				$('#fileData').dataTable({
					"aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
	                "aaSorting": [[ 0, "desc" ]],
	                "iDisplayLength": '10',
			      	'bProcessing'    : true,
			      	'bServerSide'    : true,
			      	'sAjaxSource'    : '<?php echo base_url(); ?>index.php/admin/getsalesdatatableajax',
			      	
				    'fnServerData': function(sSource, aoData, fnCallback) {
						aoData.push( { "name": "<?php echo $this->security->get_csrf_token_name(); ?>", "value": "<?php echo $this->security->get_csrf_hash() ?>" } );
						$.ajax
						({
						      'dataType': 'json',
						      'type'    : 'POST',
						      'url'     : sSource,
						      'data'    : aoData,
						      'success' : fnCallback
						});
				     },
				     
				     "oTableTools": {
				    	 	"sSwfPath" : "<?php echo $this->config->base_url(); ?>assets/admin/media/swf/copy_csv_xls_pdf.swf",
				    	 	"aButtons": [
					    	 				"csv",
					    	 				"xls",
					    	 				{
					    	 					"sExtends": "pdf",
					    	 					"sPdfOrientation": "landscape",
					    	 					"sPdfMessage": "Your custom message would go here."
					    	 				},
					    	 				"print"
				    	 				]
						},
					"aoColumns": [ { "mRender": format_date },null,null,null,{ "bSearchable": false },{ "bSearchable": false },{ "bSearchable": false }, { "mRender": status },{ "bSortable": false }],
					"fnFooterCallback": function ( nRow, aaData, iStart, iEnd, aiDisplay ) {
													var rTotal = 0, pTotal = 0, bTotal = 0;
													for ( var i=0 ; i<aaData.length ; i++ )
													{
														rTotal += aaData[ aiDisplay[i] ][4]*1;
														pTotal += aaData[ aiDisplay[i] ][5]*1;
														bTotal += aaData[ aiDisplay[i] ][6]*1;
													}
													
													var nCells = nRow.getElementsByTagName('th');
													nCells[4].innerHTML = parseFloat(rTotal).toFixed(2);
													nCells[5].innerHTML = parseFloat(pTotal).toFixed(2);
													nCells[6].innerHTML = parseFloat(bTotal).toFixed(2);
												 }
					}).columnFilter({ "aoColumns": [
														{ type: "text", bRegex:true },
														{ type: "text", bRegex:true },
														{ type: "text", bRegex:true },
														{ type: "text", bRegex:true },
														{ type: "text", bRegex:true },
														null, null,
														{ type: "select", values: [ '<?php echo $this->lang->line('sale_paid'); ?>','<?php echo $this->lang->line('sale_partially_paid'); ?>','<?php echo $this->lang->line('sale_pending'); ?>', '<?php echo $this->lang->line('sale_overdue'); ?>', '<?php echo $this->lang->line('sale_cancelled'); ?>'] },
														null
                                                      
                                                  ]});

				   
				$('#fileData').on("click", ".st", function(){
				  inv_id = $(this).attr('id');
				});
				
				var inv_st;
				$('#myModal').on('show.bs.modal', function () {
					inv_st = $('.label'+inv_id).text();
					if(inv_st == '<?php echo $this->lang->line('sale_paid'); ?>') {
						var r = confirm("<?php echo $this->lang->line('sale_paid_status_change'); ?>");
						if (r == false) {
							return false;
						} 
					}
					$('#new_status').val(inv_st); 
				});

				$('#myModal').on("click", "#update_status", function(){
					$('#update_status').text('Loading...');
					var new_status = $('#new_status').val();

					if(new_status != inv_st) {

						$.ajax({
							  type: "post",
							  url: "<?php echo base_url(); ?>index.php/admin/salesUpdateStatus",
							  data: { id: inv_id, status: new_status, <?php echo $this->security->get_csrf_token_name(); ?>: '<?php echo $this->security->get_csrf_hash() ?>' },
							  success: function(data) {
								location.reload();
							  },
							  error: function(){
		       					alert('<?php echo $this->lang->line('ajax_error'); ?>');
								$('#update_status').text('<?php echo $this->lang->line('update'); ?>');
						
		    				  }
						});
					} else { 
						alert('<?php echo $this->lang->line('sale_same_status'); ?>'); 
						$(this).text('<?php echo $this->lang->line('sale_update'); ?>'); 
						return false; 
					}
				});

				$('#fileData').on('click', '.add_payment', function() {
					var vid = $(this).attr('id');
					var cid = $(this).attr('data-customer');
					$('#vid').val(vid);
					$('#cid').val(cid);
					$('#payModal').modal();
					return false;
				});


				$('#payModal').on('click', '#add-payment', function() {
					$(this).text('Loading...');
					var vid = $('#vid').val();
					var cid = $('#cid').val();
					var note = $('#note').val();
					var amount = $('#amount').val();
					if(amount != '') {
						$.ajax({
							  type: "post",
							  url: "<?php echo base_url(); ?>index.php/admin/saleAddPayment",
							  data: { invoice_id: vid, customer_id: cid, amount: amount, note: note, <?php echo $this->security->get_csrf_token_name(); ?>: '<?php echo $this->security->get_csrf_hash() ?>' },
							  success: function(data) {location.reload();},
							  error: function(){alert('<?php echo $this->lang->line('ajax_error'); ?>');}
							  
						});
					} else { 
						alert('<?php echo $this->lang->line('no_amount'); ?>'); $(this).text('<?php echo $this->lang->line('add_payment'); ?>'); 
						return false; 
					}
				});

				$('#fileData').on('click', '.email_inv', function() {
					var vid = $(this).attr('id');
					var cid = $(this).attr('data-customer');
					$.getJSON( "<?php echo base_url(); ?>index.php/admin/saleGetCE", { id: cid, <?php echo $this->security->get_csrf_token_name(); ?>: '<?php echo $this->security->get_csrf_hash() ?>' }).done(function( json ) {
						$('#customer_email').val(json.ce);
					});
					
					$('#emailModalLabel').text('<?php echo $this->lang->line("customer_email")." ".$this->lang->line("sale_invoice")." ".$this->lang->line("sale_no"); ?> '+vid);
					$('#subject').val('<?php echo $this->lang->line("sale_invoice")." from ".SITE_NAME; ?>');
					$('#inv_id').val(vid);
					$('#emailModal').modal();
					return false;
				});

				$('#emailModal').on('click', '#email_now', function() {
					$(this).text('Sending...');
					var vid = $('#inv_id').val();
					var to = $('#customer_email').val();
					var subject = $('#subject').val();
					var note = $('#message').val();

					if(to != '') {
						$.ajax({
							  type: "post",
							  url: "<?php echo base_url(); ?>index.php/admin/saleEmail",
							  data: { id: vid, to: to, subject: subject, note: note, <?php echo $this->security->get_csrf_token_name(); ?>: '<?php echo $this->security->get_csrf_hash() ?>' },
							  success: function(data) {
								alert(data);
							  },
							  error: function(){
		       					alert('<?php echo $this->lang->line('ajax_error'); ?>');
		    				  }
							  
						});
					} else { alert('<?php echo $this->lang->line('to'); ?>'); }
					$('#emailModal').modal('hide');
					$(this).text('<?php echo $this->lang->line('send_email'); ?>');
					return false;
					
				});
				
			});
	</script>
		
	
	<div class="span10 content">

		<?php echo $hugemenu ?>
		
		<h1><?php echo lang('sale_invoices');?></h1>
		
		<?php if($success_message) { echo "<div class=\"alert alert-success\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>" . $success_message . "</div><br />"; } ?>
		<?php if($message) { echo "<div class=\"alert alert-danger\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>" . $message . "</div><br />"; } ?>
		

		<div>
			<table id="fileData" cellpadding=0 cellspacing=10 class="table table-bordered table-condensed table-hover table-striped" style="margin-bottom: 5px;">
		      <thead>
		        <tr class="active">
		          <th><?php echo $this->lang->line("sale_date"); ?></th>
		          <th><?php echo $this->lang->line("sale_reference_no"); ?></th>
		          <th><?php echo $this->lang->line("sale_created_by"); ?></th>
		          <th><?php echo $this->lang->line("sale_customer"); ?></th>
		          <th><?php echo $this->lang->line("sale_total"); ?></th>
		          <th><?php echo $this->lang->line("sale_paid"); ?></th>
		          <th><?php echo $this->lang->line("sale_balance"); ?></th>
		          <th><?php echo $this->lang->line("sale_status"); ?></th>
		          <th style="width:125px; text-align:center;"><?php echo $this->lang->line("sale_actions"); ?></th>
		        </tr>
		      </thead>
		      <tbody>
		        <tr>
		          <td colspan="9" class="dataTables_empty">Loading data from server</td>
		        </tr>
		      </tbody>
		      <tfoot>
		        <tr>
		          <th><?php echo $this->lang->line("sale_date"); ?> [yyyy-mm-dd]</th>
		          <th><?php echo $this->lang->line("sale_reference_no"); ?></th>
		          <th><?php echo $this->lang->line("sale_created_by"); ?></th>
		          <th><?php echo $this->lang->line("sale_customer"); ?></th>
		          <th><?php echo $this->lang->line("sale_total"); ?></th>
		          <th><?php echo $this->lang->line("sale_paid"); ?></th>
		          <th><?php echo $this->lang->line("sale_balance"); ?></th>
		          <th><?php echo $this->lang->line("sale_status"); ?></th>
		          <th style="width:125px; text-align:center;"><?php echo $this->lang->line("sale_actions"); ?></th>
		        </tr>
		      </tfoot>
		    </table>
    		<p><a href="<?php echo site_url('admin/salesAddInvoice');?>" class="btn btn-primary"><?php echo $this->lang->line("sale_add_invoice"); ?></a></p>
    		
		    <div class="control-group modal fade" id="payModal" tabindex="-1" role="dialog" aria-labelledby="payModalLabel" aria-hidden="true">
  				<div class="modal-dialog">
				    <div class="modal-content">
				      <div class="modal-header">
				        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				        <h4 class="modal-title" id="myModalLabel"><?php echo $this->lang->line('sale_add_payment'); ?></h4>
				      </div>
				      <div class="modal-body">
				        <div class="control-group">
				          <label class="control-label" for="amount"><?php echo $this->lang->line("sale_amount_paid"); ?></label>
				          <div class="controls"> <?php echo form_input('amount', '', 'class="input-block-level" id="amount"');?> </div>
				        </div>
				        <div class="control-group">
				          <label class="control-label" for="note"><?php echo $this->lang->line("note"); ?></label>
				          <div class="controls"> <?php echo form_textarea('note', '', 'class="input-block-level" id="note" style="height:100px;"');?> </div>
				        </div>
				        <input type="hidden" name="cid" value="" id="cid" />
				        <input type="hidden" name="vid" value="" id="vid" />
				      </div>
				      <div class="modal-footer">
				        <button class="btn" data-dismiss="modal" aria-hidden="true"><?php echo $this->lang->line('sale_close'); ?></button>
				        <button class="btn btn-primary" id="add-payment"><?php echo $this->lang->line('sale_add_payment'); ?></button>
				      </div>
				    </div>
				</div>
			</div>
		
			<div class="control-group modal fade" id="emailModal" tabindex="-1" role="dialog" aria-labelledby="emailModalLabel" aria-hidden="true">
  				<div class="modal-dialog">
				    <div class="modal-content">
				    
				      <div class="modal-header">
				        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				        <h4 class="modal-title" id="emailModalLabel"></h4>
				      </div>
				      
				      <div class="modal-body">
				        <div class="form-group">
				  			<label for="from"><?php echo $this->lang->line("from"); ?></label>
				  			<div class="controls"> <strong><?php echo $from_name; echo " &lt;".$from_email."&gt;"; /*form_input($from_name, '', 'class="text"'); */ ?></strong></div>
						</div>
						<div class="form-group">
				  			<label for="customer_email"><?php echo $this->lang->line("to"); ?></label>
				  			<div class="controls"> <?php echo form_input('to', '', 'class="form-control" id="customer_email"');?></div>
						</div>
						<div class="form-group">
				  			<label for="subject"><?php echo $this->lang->line("subject"); ?></label>
				  			<div class="controls">
				  			<?php echo form_input('subject', '', 'class="form-control" id="subject"');?> </div>
						</div>
						<div class="form-group">
				  			<label for="message"><?php echo $this->lang->line("message"); ?></label>
				  			<div class="controls"> <?php echo form_textarea('note', $this->lang->line("find_attachment"), 'id ="message" class="form-control" placeholder="'.$this->lang->line("add_note").'" rows="3" style="margin-top: 10px; height: 100px;"');?> </div>
						</div>
			      		<input type="hidden" id="qu_id" value="" />  
			      	</div>
			      	
			      	<div class="modal-footer">
			        	<button class="btn" data-dismiss="modal" aria-hidden="true"><?php echo $this->lang->line('sale_close'); ?></button>
			        	<button class="btn btn-primary" id="email_now"><?php echo $this->lang->line('send_email'); ?></button>
			      	</div>
				</div>
			</div>
		</div>
		
	</div><!--/content-->
	<script src="<?php echo $this->config->base_url(); ?>assets/admin/js/jquery-ui-1.10.2.custom.min.js"></script> <!-- jQuery UI -->
	<script src="<?php echo $this->config->base_url(); ?>assets/admin/js/sparklines.js"></script>
	<script src="<?php echo $this->config->base_url(); ?>assets/admin/js/fullcalendar.min.js"></script> <!-- Full Google Calendar - Calendar -->
	<script src="<?php echo $this->config->base_url(); ?>assets/admin/js/bootstrap-datetimepicker.min.js"></script> <!-- Date picker -->
	<script src="<?php echo $this->config->base_url(); ?>assets/admin/js/DT_bootstrap.js"></script> <!-- jQuery UI -->
	<script src="<?php echo $this->config->base_url(); ?>assets/admin/js/jquery.prettyPhoto.js"></script> <!-- prettyPhoto -->
	 
<?php echo $footer ?>